package com.zs.zcerp.salary.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.zcerp.entrance.service.IEpMainService;
import com.zs.zcerp.remark.service.CommonService;
import com.zs.zcerp.salary.entity.DeptDto;
import com.zs.zcerp.salary.entity.EsDailySalary;
import com.zs.zcerp.salary.mapper.EsDailySalaryMapper;
import com.zs.zcerp.salary.service.IEsDailySalaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@DS("oracle2")
public class EsDailySalaryServiceImpl extends ServiceImpl<EsDailySalaryMapper, EsDailySalary> implements IEsDailySalaryService {

    private final static Logger logger = LoggerFactory.getLogger(EsDailySalaryServiceImpl.class);

    final static SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private CommonService commonService;

    @Autowired
    private IEpMainService epMainService;

    @Override
    public List<DeptDto> deptLists(Integer orgId) {
        List<DeptDto> list = baseMapper.deptLists(orgId);
        for (DeptDto d : list) {
            d.setDeptId(d.getOrgId() + d.getDeptNo());
        }
        return list;
    }

    @Override
    public List<DeptDto> deptList(Integer orgId) {
        return baseMapper.deptLists(orgId);
    }

    @Override
    public List<DeptDto> orgList() {
        return baseMapper.orgList();
    }

    @Override
    public List<EsDailySalary> deptSalaryListLatest() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        List<EsDailySalary> list = new ArrayList<>();
        int di = -1;
        while (list.isEmpty()) {
            list= baseMapper.empLists(c.getTime());
            c.add(Calendar.DATE, di);
            di--;
        }

        return list;

    }

    @Override
    public List<EsDailySalary> deptSalaryListLatestSelected(JSONArray deptIds) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        List<EsDailySalary> list = new ArrayList<>();
        int di = -1;
        JSONArray orgIds = new JSONArray();
        JSONArray deptNos = new JSONArray();
        for (int i = 0; i < deptIds.size(); i++) {
            try {
                String str = deptIds.getString(i);
                orgIds.add(Integer.parseInt(str.substring(0, 4)));
                deptNos.add(str.substring(4));
            } catch (NumberFormatException e) {
                logger.error(e.toString());
            }
        }

        while (list.isEmpty()) {
            list= baseMapper.empDeptLists(orgIds, deptNos, c.getTime());
            c.add(Calendar.DATE, di);
            di--;
        }

        return list;
    }

    @Override
    public List<EsDailySalary> deptSalaryListSelected(JSONArray deptIds) {
        Integer orgId = Integer.parseInt(deptIds.getString(0).substring(0, 4));
        JSONArray deptNos = new JSONArray();
        for (int i = 0; i < deptIds.size(); i++) {
            try {
                String str = deptIds.getString(i);
                deptNos.add(str.substring(4));
            } catch (NumberFormatException e) {
                logger.info("deptSalaryListLatestSelected参数有误");
            }
        }
        return baseMapper.empDeptSelectedLists(orgId, deptNos);
    }

    @Override
    public List<EsDailySalary> deptSalaryList(Integer orgId, JSONArray depts) {
        return baseMapper.empDeptSelectedLists(orgId, depts);
    }

    @Override
    public List<EsDailySalary> deptGroupSalaryList(Integer groupId) {
        Integer orgId = 0;
        JSONArray deptNos = new JSONArray();
        if (groupId == 120101) {
            orgId = 1201;
            deptNos.add("0302");
            deptNos.add("0303");
            deptNos.add("0306");
            deptNos.add("0307");
        }
        if (groupId == 120102) {
            orgId = 1201;
            deptNos.add("0450");
            deptNos.add("0451");
            deptNos.add("0452");
            deptNos.add("0605");
        }
        return baseMapper.empDeptSelectedLists(orgId, deptNos);
    }

    @Override
    public List<EsDailySalary> findByEmpNo(String empNo, Date startTime, Date endTime) {
        return baseMapper.findByEmpNo(empNo,startTime, endTime);
    }

}
